package com.demo.core.service.base.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import com.demo.common.StringUtils;
import com.demo.core.dao.AuditDao;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;
import com.demo.core.model.Audit;
import com.demo.core.service.base.AuditService;

/**
 * 站点审计服务实现类
 * 
 * @author ryuu.kk
 *
 */
public class AuditServiceImpl implements AuditService {

	/**
	 * 配置数据访问层Dao
	 */
	private AuditDao auditDao;
	
	@Override
	@Transactional
	public Long save(Audit audit) {
		Long id = auditDao.insert(audit);
		return id;
	}
	
	@Override
	public boolean exists(Long id) {
		return auditDao.exists(id);
	}
	
	@Override
	public void delete(Long id) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("auditId", id);
		auditDao.delete(id);
	}
	
	@Override
	public Audit find(Long id) {
		return auditDao.select(id);
	}

	@Override
	public ResultSearchResult<Audit> search(Long siteId, SearchArgumentSupport args) {
		return search(siteId, null, null, args);
	}

	@Override
	public ResultSearchResult<Audit> search(Long siteId, String type, SearchArgumentSupport args) {
		return search(siteId, null, type, args);
	}
	
	@Override
	public ResultSearchResult<Audit> search(Long siteId, String userName, String type, SearchArgumentSupport sas) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		if (!StringUtils.isEmpty(userName)) {
			params.put("userName", userName);
		}
		if (!StringUtils.isEmpty(type)) {
			params.put("type", type);
		}
		ResultSearchResult<Audit> result = auditDao.search(params, sas);
		return result;
	}
	
	public void setAuditDao(AuditDao auditDao) {
		this.auditDao = auditDao;
	}
}